package o;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.SparseIntArray;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import o.eru.b;

final class err {
    private final int[] ˊ;
    private final SparseIntArray ˏ;
    private final List<b> ॱ;

    class a implements Comparator<a> {
        private int ʻ;
        private int ʼ;
        private int ʽ;
        final /* synthetic */ err ˊ;
        private int ˋ;
        private int ˎ;
        private int ˏ;
        private int ॱ;
        private int ॱॱ;

        public /* synthetic */ int compare(Object obj, Object obj2) {
            return ˏ((a) obj, (a) obj2);
        }

        a(err o_err, int i, int i2) {
            this.ˊ = o_err;
            this.ˎ = i;
            this.ˋ = i2;
            ˋ();
        }

        a(err o_err) {
            this.ˊ = o_err;
        }

        public int ˏ(a aVar, a aVar2) {
            return aVar2.ˏ() - aVar.ˏ();
        }

        int ˏ() {
            return (((this.ॱ - this.ˏ) + 1) * ((this.ʻ - this.ʽ) + 1)) * ((this.ʼ - this.ॱॱ) + 1);
        }

        boolean ˎ() {
            return ˊ() > 1;
        }

        int ˊ() {
            return (this.ˋ - this.ˎ) + 1;
        }

        void ˋ() {
            this.ॱॱ = 255;
            this.ʽ = 255;
            this.ˏ = 255;
            this.ʼ = 0;
            this.ʻ = 0;
            this.ॱ = 0;
            for (int i = this.ˎ; i <= this.ˋ; i++) {
                int i2 = this.ˊ.ˊ[i];
                int red = Color.red(i2);
                int green = Color.green(i2);
                i2 = Color.blue(i2);
                if (red > this.ॱ) {
                    this.ॱ = red;
                }
                if (red < this.ˏ) {
                    this.ˏ = red;
                }
                if (green > this.ʻ) {
                    this.ʻ = green;
                }
                if (green < this.ʽ) {
                    this.ʽ = green;
                }
                if (i2 > this.ʼ) {
                    this.ʼ = i2;
                }
                if (i2 < this.ॱॱ) {
                    this.ॱॱ = i2;
                }
            }
        }

        a ॱ() {
            if (ˎ()) {
                int ʼ = ʼ();
                a aVar = new a(this.ˊ, ʼ + 1, this.ˋ);
                this.ˋ = ʼ;
                ˋ();
                return aVar;
            }
            throw new IllegalStateException("Can not split a box with only 1 color");
        }

        int ॱॱ() {
            int i = this.ॱ - this.ˏ;
            int i2 = this.ʻ - this.ʽ;
            int i3 = this.ʼ - this.ॱॱ;
            if (i >= i2 && i >= i3) {
                return -3;
            }
            if (i2 < i || i2 < i3) {
                return -1;
            }
            return -2;
        }

        int ʼ() {
            int ॱॱ = ॱॱ();
            this.ˊ.ˏ(ॱॱ, this.ˎ, this.ˋ);
            Arrays.sort(this.ˊ.ˊ, this.ˎ, this.ˋ + 1);
            this.ˊ.ˏ(ॱॱ, this.ˎ, this.ˋ);
            int ˎ = ˎ(ॱॱ);
            for (int i = this.ˎ; i <= this.ˋ; i++) {
                int i2 = this.ˊ.ˊ[i];
                switch (ॱॱ) {
                    case -3:
                        if (Color.red(i2) < ˎ) {
                            break;
                        }
                        return i;
                    case -2:
                        if (Color.green(i2) < ˎ) {
                            break;
                        }
                        return i;
                    case -1:
                        if (Color.blue(i2) <= ˎ) {
                            break;
                        }
                        return i;
                    default:
                        break;
                }
            }
            return this.ˎ;
        }

        b ʻ() {
            int i = 0;
            int i2 = 0;
            int i3 = 0;
            int i4 = 0;
            for (int i5 = this.ˎ; i5 <= this.ˋ; i5++) {
                int i6 = this.ˊ.ˊ[i5];
                int i7 = this.ˊ.ˏ.get(i6);
                i += i7;
                i4 += Color.red(i6) * i7;
                i3 += Color.green(i6) * i7;
                i2 += Color.blue(i6) * i7;
            }
            if (i == 0) {
                return new b(0, 0, 0, 0);
            }
            return new b(Math.round(((float) i4) / ((float) i)), Math.round(((float) i3) / ((float) i)), Math.round(((float) i2) / ((float) i)), i);
        }

        int ˎ(int i) {
            switch (i) {
                case -2:
                    return (this.ʽ + this.ʻ) / 2;
                case -1:
                    return (this.ॱॱ + this.ʼ) / 2;
                default:
                    return (this.ˏ + this.ॱ) / 2;
            }
        }
    }

    private err(ers o_ers, int i) {
        int i2;
        int i3 = 0;
        int ˋ = o_ers.ˋ();
        int[] ˊ = o_ers.ˊ();
        int[] ˏ = o_ers.ˏ();
        this.ˏ = new SparseIntArray(ˋ);
        for (i2 = 0; i2 < ˊ.length; i2++) {
            this.ˏ.append(ˊ[i2], ˏ[i2]);
        }
        this.ˊ = new int[ˋ];
        int length = ˊ.length;
        i2 = 0;
        ˋ = 0;
        while (i2 < length) {
            int i4 = ˋ + 1;
            this.ˊ[ˋ] = ˊ[i2];
            i2++;
            ˋ = i4;
        }
        if (ˋ <= i) {
            this.ॱ = new ArrayList();
            int[] iArr = this.ˊ;
            ˋ = iArr.length;
            while (i3 < ˋ) {
                i4 = iArr[i3];
                this.ॱ.add(new b(i4, this.ˏ.get(i4)));
                i3++;
            }
            return;
        }
        this.ॱ = ˏ(ˋ - 1, i);
    }

    static err ॱ(Bitmap bitmap, int i) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] iArr = new int[(width * height)];
        bitmap.getPixels(iArr, 0, width, 0, 0, width, height);
        return new err(new ers(iArr), i);
    }

    List<b> ˎ() {
        return this.ॱ;
    }

    private List<b> ˏ(int i, int i2) {
        Collection priorityQueue = new PriorityQueue(i2, new a(this));
        priorityQueue.offer(new a(this, 0, i));
        ˋ(priorityQueue, i2);
        return ˏ(priorityQueue);
    }

    private void ˋ(PriorityQueue<a> priorityQueue, int i) {
        while (priorityQueue.size() < i) {
            a aVar = (a) priorityQueue.poll();
            if (aVar != null && aVar.ˎ()) {
                priorityQueue.offer(aVar.ॱ());
                priorityQueue.offer(aVar);
            } else {
                return;
            }
        }
    }

    private List<b> ˏ(Collection<a> collection) {
        List arrayList = new ArrayList(collection.size());
        for (a ʻ : collection) {
            arrayList.add(ʻ.ʻ());
        }
        return arrayList;
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void ˏ(int r5, int r6, int r7) {
        /*
        r4 = this;
        switch(r5) {
            case -3: goto L_0x0003;
            case -2: goto L_0x0004;
            case -1: goto L_0x001f;
            default: goto L_0x0003;
        };
    L_0x0003:
        return;
    L_0x0004:
        if (r6 > r7) goto L_0x0003;
    L_0x0006:
        r0 = r4.ˊ;
        r0 = r0[r6];
        r1 = r4.ˊ;
        r2 = r0 >> 8;
        r2 = r2 & 255;
        r3 = r0 >> 16;
        r3 = r3 & 255;
        r0 = r0 & 255;
        r0 = android.graphics.Color.rgb(r2, r3, r0);
        r1[r6] = r0;
        r6 = r6 + 1;
        goto L_0x0004;
    L_0x001f:
        if (r6 > r7) goto L_0x0003;
    L_0x0021:
        r0 = r4.ˊ;
        r0 = r0[r6];
        r1 = r4.ˊ;
        r2 = r0 & 255;
        r3 = r0 >> 8;
        r3 = r3 & 255;
        r0 = r0 >> 16;
        r0 = r0 & 255;
        r0 = android.graphics.Color.rgb(r2, r3, r0);
        r1[r6] = r0;
        r6 = r6 + 1;
        goto L_0x001f;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.err.ˏ(int, int, int):void");
    }
}
